'''
    给一组离散点列，求一条曲线，把这些点按次序连接起来（要经过这些点，而拟合就不一定）
'''

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
x=np.array([0,3,5,7,9,11,12,13,14,15])
y=np.array([0,1.2,1.7,2.0,2.1,2.0,1.8,1.2,1.0,1.6])
# 线性插值
# 可以看出在x=14处光滑性较差
f_linear=interpolate.interp1d(x,y)
xx=np.linspace(0,15,180)
yy=f_linear(xx)
# B-spline插值(三次样条插值) 曲线更加光滑
tck = interpolate.splrep(x, y)
y_bspline = interpolate.splev(xx, tck)
plt.xlabel("X")
plt.ylabel("Y")
plt.plot(x,y,"o","original data")
plt.plot(xx,yy,label="Linear interpolation")
plt.plot(xx,y_bspline,label="B-spline interpolation")
plt.legend()
plt.show()

